var paymentCalculateStore;
function ready() {
	//薪资模板下接选项数据信息-------------------------------------------------------------------------------------
	var payClassStore = new Ext.data.Store({
		url : 'payclassAction.action?actionType=0',
		reader : new Ext.data.JsonReader({
				root : 'result.result',
				id : 'payClassStore'
			}, [{
				name : 'pay_class_id',
				mapping : 'pay_class_id'
			}, {
				name : 'pay_class_name',
				mapping : 'pay_class_name'
			}])
	});
	payClassStore.load();
	
	// 薪资查询条件FormPanel-------------------------------------------------------------------------------------
	var conditionForm = new Ext.form.FormPanel({
				frame : true,
				title : '薪资查询',
				id : 'conditionForm',
				buttonAlign : 'right',
				bodyStyle : 'padding:5px 5px 0',
				items : [{
					layout : 'column',
					columnWidth : 1,
					items : [{
								columnWidth : 0.5,
								layout : 'form',
								items : [{
											xtype : 'combo',
											store : payClassStore,
											valueField : "pay_class_id",
											displayField : "pay_class_name",
											fieldLabel : '薪资模板名称',
											name : 'pay_class',
											id : 'pay_class',
											anchor : '90%',
											emptyText : '请选择薪资模板',
											triggerAction : 'all',
											editable : false
										}, {
											fieldLabel : '薪资月份从',
											name : 'monthStart',
											id : 'monthStart',
											anchor : '90%',
											xtype : 'textfield'
										}]
							}, {
								columnWidth : 0.5,
								layout : 'form',
								items : [{
											xtype : 'textfield',
											fieldLabel : '雇员姓名',
											anchor : '90%',
											name : 'name',
											id : 'name'
										}, {
											fieldLabel : '薪资月份到',
											name : 'monthEnd',
											id : 'monthEnd',
											anchor : '90%',
											xtype : 'textfield'
										}]
							}, {
								columnWidth : 0.5,
								layout : 'form',
								items : [{
									xtype : 'button',
									text : '查询',
									handler : function() {
										payEmpStore.on('beforeload',
												function() {
													payEmpStore.baseParams = {
														queryType : "payTotal",
														'filter.payTypeId' : Ext
																.getCmp('pay_class')
																.getValue(),
														'filter.name' : Ext
																.getCmp('name')
																.getValue(),
														'filter.monthStart' : Ext
																.getCmp('monthStart')
																.getValue(),
														'filter.monthEnd' : Ext
																.getCmp('monthEnd')
																.getValue()
													}
												});
										payEmpStore.load();
									}
								}]
							}]
				}]
			});
	conditionForm.render(Ext.getBody());
	// 雇员薪资统计列表GridPannel--------------------------------------------------------------------------------
	var payEmpStore = new Ext.data.Store({
				url : 'payEmpdataAction.action?actionType=0',
				reader : new Ext.data.JsonReader({
							root : 'payTotalResult.result',
							totalProperty : 'payTotalResult.totalCount'
						}, [{
									name : 'hisId',
									mapping : 'hisId'
								},{
									name : 'employeeId',
									mapping : 'employeeId'
								}, {
									name : 'name',
									mapping : 'name'
								}, {
									name : 'payClassId',
									mapping : 'payClassId'
								}, {
									name : 'payClassName',
									mapping : 'payClassName'
								}, {
									name : 'period',
									mapping : 'period'
								}, {
									name : 'salary',
									mapping : 'salary'
								}])
			});

	var payEmpCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
				header : '雇员ID',
				dataIndex : 'employeeId',
				hidden : true
			}, {
				header : '历史id',
				dataIndex : 'hisId',
				hidden : true
			}, {
				header : '薪资类别id',
				dataIndex : 'payClassId',
				hidden : true
			}, {
				header : '雇员姓名',
				dataIndex : 'name'
			}, {
				header : '薪资模板',
				dataIndex : 'payClassName'
			}, {
				header : '年度月份',
				dataIndex : 'period'
			}, {
				header : '薪资额',
				dataIndex : 'salary'
			}, {
				header : '明细查看',
				dataIndex : '',
				renderer : function(v, p, record) {
					return "<input type='button' class='x-btn' value='明细查看' onClick=\"ShowPayResult('"
							+ record.data.payClassId
							+ "','"
							+ record.data.payClassName
							+ "','"
							+ record.data.employeeId
							+ "','"
							+ record.data.period
							+ "','" 
							+ record.data.hisId +"')\">";
				}
			}]);

	var payEmpGrid = new Ext.grid.GridPanel({
				autoScroll : true,
				height : document.body.clientHeight
						- Ext.getCmp('conditionForm').getInnerHeight()
						- Ext.getCmp('conditionForm').getFrameHeight(),

				id : 'payEmpGrid',
				region : 'center',
				cm : payEmpCm,
				store : payEmpStore,
				loadMask : true,
				loadMask : {
					msg : '正在加载数据...'
				},
				frame : true,
				clicksToEdit : 1,
				bbar : new Ext.PagingToolbar({
							pageSize : pageSize,
							store : payEmpStore,
							displayInfo : true,
							dispalyMsg : '显示第{0}条到{1}条记录，一共{2}条',
							emptyMsg : '没有记录',
							items:[{xtype : "tbseparator"}, 
								{xtype : 'button',text:'数据导出', handler:function(value){
									window.location = "payEmpdataActionForExcelExport.action?actionType=5&close=1&filter.payTypeId="+Ext.getCmp('pay_class').getValue()
									+"&filter.name="+Ext.getCmp('name').getValue()
									+"&filter.monthStart="+Ext.getCmp('monthStart').getValue()
									+"&filter.monthEnd="+Ext.getCmp('monthEnd').getValue();
								}
							}]
						})
			});
	payEmpGrid.render(Ext.getBody());
}

// 显示员工薪资明细信息
function ShowPayResult(payClassId, payClassName, employeeId, period, hisId) {
	var paymentCalculatePanel = new Ext.form.FormPanel({
				frame : true,
				region : 'north',
				anchor : '90%',
				buttonAlign : 'right',
				id : 'employMessage',
				bodyStyle : 'padding:5px 5px 0',
				url : '',
				items : [{
							layout : 'column',
							items : [{
										columnWidth : .5,
										layout : 'form',
										items : [{
													anchor : '90%',
													layout : 'form',
													xtype : 'label',
													text : '薪资模板：'
															+ payClassName
												}]
									}, {
										columnWidth : .5,
										layout : 'form',
										items : [{
													fieldLabel : '薪资对应年月',
													anchor : '90%',
													layout : 'form',
													xtype : 'label',
													text : '薪资对应年月：' + period
												}]
									}]
						}]
			});

	// 获取表头信息及所对应字段信息---------------------------------------------------------------------------------
	var columnModelResponseText;
	var storeFieldResponseText;

	var conn = Ext.lib.Ajax.getConnectionObject().conn;
	conn.open("POST", 'dynamicColumnAction.action', false);
	conn.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded;charset=UTF-8");
	conn.send("filter.payItemId=" + payClassId + "&filter.hisId=" + hisId + "&queryType=columnModelHis");
	var responseTextVale = Ext.util.JSON.decode(conn.responseText);
	columnModelResponseText = Ext.encode(responseTextVale.columnModelList);
	columnModelResponseText = columnModelResponseText.substr(1,
			columnModelResponseText.length - 2);
	var reg_header = new RegExp("\"header\"", "g");
	var reg_dataIndex = new RegExp("\"dataIndex\"", "g");
	var reg = new RegExp("\"", "g");
	columnModelResponseText = columnModelResponseText.replace(reg_header,
			"header");
	columnModelResponseText = columnModelResponseText.replace(reg_dataIndex,
			"dataIndex");
	columnModelResponseText = columnModelResponseText.replace(reg, "'");

	conn.open("POST", 'dynamicColumnAction.action', false);
	conn.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded;charset=UTF-8");
	conn.send("payClassId=" + payClassId + "&queryType=storeField");
	var responseTextVale = Ext.util.JSON.decode(conn.responseText);
	storeFieldResponseText = Ext.encode(responseTextVale.storeFieldList);
	storeFieldResponseText = storeFieldResponseText.substr(1,
			storeFieldResponseText.length - 2);
	var reg_name = new RegExp("\"name\"", "g");
	var reg_mapping = new RegExp("\"mapping\"", "g");
	storeFieldResponseText = storeFieldResponseText.replace(reg_name, "name");
	storeFieldResponseText = storeFieldResponseText.replace(reg_mapping,
			"mapping");
	storeFieldResponseText = storeFieldResponseText.replace(reg, "'");

	// 查询显示的表格
	var cm = new Ext.grid.ColumnModel(eval("[new Ext.grid.RowNumberer(), "
			+ "{header:'',dataIndex:'payId',hidden:true},"
			+ "{header:'',dataIndex:'his_id',hidden:true},"
			+ "{header:'',dataIndex:'employee_id',hidden:true},"
			+ "{header:'员工姓名',dataIndex:'name'},"
			+ "{header:'工资月份',dataIndex:'period'}," + columnModelResponseText
			+ "]"));

	// 数据源
	paymentCalculateStore = new Ext.data.Store({
				url : 'payEmpdataAction.action',
				reader : new Ext.data.JsonReader({
							root : 'payDataList',
							id : 'paymentStore'
						}, eval("[" + "{name:'payId',mapping:'payId'},"
								+ "{name:'his_id',mapping:'his_id'},"
								+ "{name:'employee_id',mapping:'employee_id'},"
								+ "{name:'name',mapping:'name'},"
								+ "{name:'period',mapping:'period'},"
								+ storeFieldResponseText + "]"))
			});
	paymentCalculateStore.on('beforeload', function() {
				paymentCalculateStore.baseParams = {
					queryType : "payData",
					'filter.payTypeId' : payClassId,
					'filter.employeeId' : employeeId,
					'filter.monthStart' : period
				}
			});
	paymentCalculateStore.load();
	var paymentCalculateGrid = new Ext.grid.GridPanel({
				footer : true,
				height : 280,
				width : 790,
				anchor : '90%',
				region : 'center',
				id : 'employeePaymentGrid',
				autoScroll : true,
				bbar : new Ext.PagingToolbar({
					pageSize : pageSize,
					store : paymentCalculateStore,
					displayInfo : true,
					dispalyMsg : '显示第{0}条到{1}条记录，一共{2}条',
					emptyMsg : '没有记录'
				}),
				cm : cm,
				store : paymentCalculateStore,
				loadMask : true,
				loadMask : {
					msg : '正在加载数据...'
				}
			});

	var paymentCalculateWin = new Ext.Window({
				closable : true,
				draggable : true,
				layout : 'border',
				id : 'paymentCalculateWin',
				title : '员工薪资计算',
				width : 800,
				height : 400,
				plain : true,
				items : [paymentCalculatePanel, paymentCalculateGrid]
			});

	paymentCalculateWin.show();
}